/*
 * Copyright Radosław Smogura, 2012. All rights reserved.
 *
 * The content of this file is confidential and it is intelectual property
 * of copyright owners.
 * 
 *
 * $URL: http://reprap-ide.googlecode.com/svn/trunk/RepRapIDE/RepRapProject/src/main/java/eu/softper/reprap/utils/BoundMinMaxCollector.java $
 * $Id: BoundMinMaxCollector.java 3 2012-11-19 10:58:30Z mail@smogura.eu $
 * $Revision: 3 $
 */
package eu.softper.reprap.utils;

import eu.softper.reprap.generator.NumericFactory;
import eu.softper.reprap.generator.SceneTriangle;
import java.math.BigDecimal;

/**
 *
 * @author rsmogura
 */
public class BoundMinMaxCollector<T extends Number & Comparable<T>> {
    private T minX, minY, minZ;
    private T maxX, maxY, maxZ;
    
    private NumericFactory<T> nf;
    
    protected T bigMin(T a, T b) {
        if (a == null)
            return b;
        if (a.compareTo(b) < 0)
            return a;
        else
            return b;
    }
    
    protected T bigMax(T a, T b) {
        if (a == null)
            return b;
        if (a.compareTo(b) > 0)
            return a;
        else
            return b;
    }

    public BoundMinMaxCollector(NumericFactory<T> numericFactory) {
        this.nf = numericFactory;
    }
    
    public void processTriangle(float[] v1, float[] v2, float[] v3) {
        processPoint(nf.valueOf(v1[0]), nf.valueOf(v1[1]), nf.valueOf(v1[2]));
        processPoint(nf.valueOf(v2[0]), nf.valueOf(v2[1]), nf.valueOf(v2[2]));
        processPoint(nf.valueOf(v3[0]), nf.valueOf(v3[1]), nf.valueOf(v3[2]));
    }
    
    public void processTriangle(SceneTriangle<T> t) {
        processPoint(t.getV1().getX(), t.getV1().getY(), t.getV1().getZ());
        processPoint(t.getV2().getX(), t.getV2().getY(), t.getV2().getZ());
        processPoint(t.getV3().getX(), t.getV3().getY(), t.getV3().getZ());
    }
    
    public void processPoint(T x, T y, T z) {
        minX = bigMin(minX, x);
        minY = bigMin(minY, y);
        minZ = bigMin(minZ, z);
        
        maxX = bigMax(maxX, x);
        maxY = bigMax(maxY, y);
        maxZ = bigMax(maxZ, z);
    }

    public T getMaxX() {
        return maxX;
    }

    public T getMaxY() {
        return maxY;
    }

    public T getMaxZ() {
        return maxZ;
    }

    public T getMinX() {
        return minX;
    }

    public T getMinY() {
        return minY;
    }

    public T getMinZ() {
        return minZ;
    }
}
